package com.irdstudio.apicenter.console.dm;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.springframework.stereotype.Component;

import com.irdstudio.apicenter.frame.core.jdbc.util.ConnectionManager;


@Component(value = "appWxModelDefineDao")
public class AppWxModelDefineDao {
	
	/**
	 * 根据主键查询单条记录
	 * 
	 * @param
	 * @return
	 * @throws SQLException
	 */
	public AppWxModelDefine queryWithKeys(String dxModelId) throws SQLException {
		AppWxModelDefine appWxModelDefine = null;
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn
					.prepareStatement("SELECT * FROM  wx_model_define WHERE dx_model_id=?");
			ps.setString(1, dxModelId);
			rs = ps.executeQuery();
			if (rs.next()) {
				appWxModelDefine = new AppWxModelDefine();
				appWxModelDefine.setDxModelId(rs.getString("dx_model_id"));
				appWxModelDefine.setModelName(rs.getString("MODEL_NAME"));
				appWxModelDefine.setWxModelId(rs.getString("WX_MODEL_ID"));
				appWxModelDefine.setAppId(rs.getString("APP_ID"));
				appWxModelDefine.setSendTime(rs.getString("SEND_TIME"));
			}
		} catch (SQLException e) {
			throw new SQLException("query wx_model_define WithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close2(rs, conn, ps);
		}
		return appWxModelDefine;
	}

	/**
	 * 插入单条记录
	 * 
	 * @param
	 * @return
	 * @throws SQLException
	 */
	public int save(AppWxModelDefine appWxModelDefine) throws SQLException {
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		int flag;
		try {
			String sql = "insert into wx_model_define(dx_model_id  , MODEL_NAME  , WX_MODEL_ID  , APP_ID , SEND_TIME ) values(? , ? , ? , ? , ? )";
			ps = conn.prepareStatement(sql);
			ps.setString(1, appWxModelDefine.getDxModelId());
			ps.setString(2, appWxModelDefine.getModelName());
			ps.setString(3, appWxModelDefine.getWxModelId());
			ps.setString(4, appWxModelDefine.getAppId());
			ps.setString(5, appWxModelDefine.getSendTime());
			
			flag = ps.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("insert wx_model_define is Wrong!" + e.getMessage());
		} finally {
			close1(conn, ps);
		}
		return flag;
	}

	/**
	 * 修改记录
	 * 
	 * @param
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */

	public int update(AppWxModelDefine appWxModelDefine) throws SQLException,
			ClassNotFoundException {
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		int flag;
		try {
			String sql = "update wx_model_define set model_name=? , wx_model_Id=? , app_Id=? , send_Time=? where dx_Model_Id=?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, appWxModelDefine.getModelName());
			ps.setString(2, appWxModelDefine.getWxModelId());
			ps.setString(3, appWxModelDefine.getAppId());
			ps.setString(4, appWxModelDefine.getSendTime());
			ps.setString(5, appWxModelDefine.getDxModelId());
			
			flag = ps.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("update wx_model_define WithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close1(conn, ps);
		}
		return flag;
	}

	/**
	 * 删除模块参数
	 * 
	 * @param appModuleParam
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public int delete(String dxModelId) throws SQLException,
			ClassNotFoundException {
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		int flag;
		try {
			String sql = "delete from wx_model_define where dx_model_id=? ";
			ps = conn.prepareStatement(sql);
			ps.setString(1, dxModelId);
			flag = ps.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("delete wx_model_define WithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close1(conn, ps);
		}

		return flag;
	}

	/**
	 * 关闭资源
	 * 
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close(ResultSet theRs, Statement theStmt,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (theStmt != null)
				theStmt.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 关闭资源
	 * 
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close1(Connection conn, PreparedStatement thePs) {
		try {
			if (conn != null)
				conn.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 关闭资源
	 * 
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close2(ResultSet theRs, Connection conn,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (conn != null)
				conn.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}


	

	
	
}
